package com.wcsy.myblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wcsy.myblog.dao.UserMapper;
import com.wcsy.myblog.entity.Attention;
import com.wcsy.myblog.entity.JsonResult;
import com.wcsy.myblog.entity.User;
import com.wcsy.myblog.service.IAttentionService;
import com.wcsy.myblog.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 关注信息
 */
@RestController
@RequestMapping("/attention")
@CrossOrigin
@Api(description = "关注信息")
@ApiModel
public class AttentionController {
    @Autowired
    private IAttentionService iAttentionServicel;
    @Autowired
    private IUserService iUserService;
    @Autowired
    HttpSession session;
    @Autowired
    UserMapper userMapper;

    /**
     * 显示用户的粉丝和关注用户的信息
     * @return map.get("idol") 我关注的人 attention_id,attention_name,attention_img
     *         map.get("fan") 我的粉丝  user_name user_img user_id
     */
    @ApiOperation("显示用户的粉丝和关注用户的信息")
    @GetMapping("/fun")
    public JsonResult fun(){
        Map<String, List<Attention>> map = new LinkedHashMap<>();
        User user = (User) session.getAttribute("user");
        //用户的偶像
        List<Attention> list = iAttentionServicel.list(new QueryWrapper<Attention>().eq("user_id", user.getUserId()));
        //我关注的人
        map.put("idol",list);
        //用户的粉丝
        List<Attention> fan = iAttentionServicel.list(new QueryWrapper<Attention>().eq("attention_id", user.getUserId()));
        //我的粉丝
        map.put("fan",fan);
        return new JsonResult(200,"查询成功",map);
    }
    /**
     * @apiNote 取消关注
     * */
    @ApiOperation("取消关注")
    @GetMapping("/delete")
    public JsonResult delete(  int userId){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User) session.getAttribute("user");
        boolean b = iAttentionServicel.remove(new QueryWrapper<Attention>().
                eq("attention_id", userId)
                .eq("user_id",user.getUserId()));
        if (b){
            User user1 = iUserService.getOne(new QueryWrapper<User>().eq("user_id", userId));
            //被关注者取消关注成功后粉丝数-1
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id",userId);
            User user2 = new User();
            user2.setUserFan(user1.getUserFan()-1);
            iUserService.update(user2,updateWrapper);
            //关注者取消关注成功后 关注数加-1
            UpdateWrapper<User> updateWrapper1 = new UpdateWrapper<>();
            updateWrapper1.eq("user_id",user.getUserId());
            User user3 = new User();
            user3.setUserAttention(user.getUserAttention()-1);
            iUserService.update(user3,updateWrapper1);
            return new JsonResult(200,"删除成功","");
        }else {
            return new JsonResult(500,"删除失败","");
        }
    }
    /**
     * @apiNote 添加关注和粉丝
     * @param  // 用户传过来被关注者的id
     * */
    @ApiOperation("添加关注和粉丝")
    @GetMapping("/insert")
    public JsonResult insert( int userId){
        //根据传过来来的id 查询出被关注者的所有信息
        User user1 = iUserService.getOne(new QueryWrapper<User>().eq("user_id", userId));
        //根据session获得关注者的所有信息
        User user = (User) session.getAttribute("user");
//        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", 1));
        Attention attention = new Attention();
        attention.setAttentionId(userId);
        attention.setAttentionName(user1.getUserName());
        attention.setAttentionImg(user1.getUserImg());
        attention.setUserId(user.getUserId());
        attention.setUserName(user.getUserName());
        attention.setUserImg(user.getUserImg());
        attention.setUserFan(attention.getUserFan()+1);
        try {
            iAttentionServicel.insertAttontion(attention);
            userMapper.updateFan(userId);
            //关注者添加成功后 关注数加+1
            userMapper.updateAttention((int) user1.getUserId());
            //在用户的关注列表中显示关注的用户name和用户图片
//            List<Attention> list = iAttentionServicel.list(new QueryWrapper<Attention>().eq("user_id", user.getUserId()));
            return new JsonResult(200,"添加成功","");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"添加失败","");
        }
    }
}
